<template>
  <div style="margin: 20px">
    <el-card>
      <div style="margin-bottom: 10px; display: flex">
        <el-input
          class="w-50 m-2"
          style="width: 150px"
          placeholder="请输入关键词"
          v-model="form.keywords"
        />
        <el-button style="margin-left: 10px" type="primary" @click="get"
          >关键词搜索</el-button
        >
        <el-button @click="toAdd">新建项目</el-button>
      </div>
      <el-table :data="state.tableData" :border="true" highlight-current-row>
        <el-table-column prop="id" label="ID" width="150" />
        <el-table-column prop="name" label="资源名称" width="150" />
        <el-table-column prop="frontUrl" label="前端url" width="250" />
        <el-table-column prop="backUrl" label="后端url" />
        <el-table-column prop="parentId" label="上级资源ID" />
        <el-table-column fixed="right" label="操作" width="200">
          <template v-slot="scope">
            <el-button link size="small" @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              title="是否确认删除该项？"
              confirm-button-text="是"
              cancel-button-text="否"
              @confirm="doDel(scope.row.id)"
            >
              <template #reference>
                <el-button link type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        v-model:current-page="form.current"
        v-model:page-size="form.size"
        :page-sizes="[10, 20, 50]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="state.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      v-model="dlgState.visible"
      title="新增项目"
      :width="600"
      @close="clean"
    >
      <el-form :model="dlgState.form" ref="dlg_form">
        <el-form-item label="资源ID" prop="id">
          <el-input :disabled="true" v-model="dlgState.form.id" />
        </el-form-item>
        <el-form-item label="资源名称：" prop="name">
          <el-input v-model="dlgState.form.name" />
        </el-form-item>
        <el-form-item label="前端url：" prop="frontUrl">
          <el-input v-model="dlgState.form.frontUrl" />
        </el-form-item>
        <el-form-item label="后端url：" prop="backUrl">
          <el-input v-model="dlgState.form.backUrl" />
        </el-form-item>
        <el-form-item label="上级ID：" prop="parentId">
          <el-select>
            <el-option
              v-for="(item, index) in state.tableData"
              :key="index"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelAdd">返回</el-button>
          <el-button type="primary" @click="doAdd" v-if="dlgState.isAdd">
            新增
          </el-button>
          <el-button type="primary" @click="doEdit" v-else> 保存 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import router from "@/router";
import resourceApi from "@/api/user/resource";
import { number } from "echarts";

// type definition

// tableData
const form = reactive({
  current: 1,
  keywords: "",
  size: 10,
});
const state = reactive({
  total: 0,
  tableData: [] as Array<any>,
});
const handleSizeChange = (val: number) => {
  form.size = val;
  get();
};
const handleCurrentChange = (val: number) => {
  form.current = val;
  get();
};
const get = () => {
  resourceApi.get(form).then((res) => {
    if (res.data.code === 200) {
      state.tableData = res.data.data.records;
      state.total = res.data.data.total;
    }
  });
};
// 对话框
const dlg_form = ref();
const dlgState = reactive({
  visible: false,
  isAdd: true,
  form: {
    id: 0,
    name: "",
    frontUrl: "",
    backUrl: "",
    type: 0, // 1目录 0按钮
    parentId: 0, // 父节点id
  },
});
const toAdd = () => {
  dlgState.isAdd = true;
  dlgState.visible = true;
};
const cancelAdd = () => {
  dlgState.visible = false;
};
const doAdd = () => {
  resourceApi.add(dlgState.form).then((res) => {
    if (res.status === 200) {
      ElMessage.success("操作成功");
      get();
      dlgState.visible = false;
    }
  });
};
const clean = () => {
  dlgState.form = {
    id: 0,
    name: "",
    frontUrl: "",
    backUrl: "",
    type: 0, // 1目录 0按钮
    parentId: 0, // 父节点id
  };
};

const toEdit = (row: any) => {
  dlgState.form = row;
  dlgState.isAdd = false;
  dlgState.visible = true;
  console.log(editForm.form);
};
const doEdit = () => {
  resourceApi.update(dlgState.form).then((res) => {
    console.log(res);
    if (res.status === 200) {
      ElMessage.success("保存成功");
      get();
      dlgState.visible = false;
    }
  });
};

// 菜单资源项目删除
const doDel = (id: number) => {
  resourceApi.del(id).then((res) => {
    if (res.status === 200) {
      ElMessage.success("删除成功");
      get();
    }
  });
};
// 项目编辑
const editForm = reactive({
  form: {
    name: "",
    type: 1,
    description: "",
  },
});

onMounted(() => {
  get();
});
</script>
<style lang="scss" scoped>
.tabs > .el-tabs__content {
  width: 100%;
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
.tabs {
  margin: 20px;
}
</style>
